package com.oasen.learn.usercenter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.oasen.learn.usercenter.action")
public class WebConfig extends WebMvcConfigurerAdapter{
	
//	@Bean
//	public ViewResolver viewResolver(){
//		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//		resolver.setPrefix("WEB-INFO/view/");
//		resolver.setSuffix(".jsp");
//		resolver.setExposeContextBeansAsAttributes(true);
//		return resolver;
//	}
//	@Bean
//	public TilesConfigurer tilesConfigurer(){
//		TilesConfigurer tiles = new TilesConfigurer();
//		//指定Tile定义的位置
//		tiles.setDefinitions(new String[]{
//				"/WEB-INF/layout/tiles.xml"
//		});
//		//启用刷新功能 
//		tiles.setCheckRefresh(true);
//		return tiles;
//	}
//	@Bean
//	public ViewResolver tileResolver(){
//		return new TilesViewResolver();
//	}
	@Bean
	 public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
	    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
	    viewResolver.setCharacterEncoding("UTF-8");
	    viewResolver.setTemplateEngine(templateEngine);
	    return viewResolver;
	 }
	 @Bean
	 public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) {
	    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	    templateEngine.setTemplateResolver(templateResolver);
//	    templateEngine.addDialect(new SpringSecurityDialect());
	    return templateEngine;
	 }

	 @Bean
	 public TemplateResolver templateResolver() {
	    TemplateResolver templateResolver = new ServletContextTemplateResolver();
	    templateResolver.setPrefix("/WEB-INF/views/");
	    templateResolver.setSuffix(".html");
	    templateResolver.setTemplateMode("HTML5");
	    templateResolver.setCharacterEncoding("UTF-8");
	    return templateResolver;
	}
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	@Bean 
	public MultipartResolver multipartResolver(){
		return new StandardServletMultipartResolver();
	}
}
